home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / mus / misc / mpegaudio.lha / mpegaudio / decode.c < prev    next >
C/C++ Source or Header  |  1994-03-21  |  24KB  |  678 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. decode.c
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com          *
  10.  *                                                                    *
  11.  * VERSION 3.9                                                       *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Douglas Wong,       start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 3/06/91  Douglas Wong        rename: setup.h to dedef.h            *
  17.  *                                      dfilter to defilter           *
  18.  *                                      dwindow to dewindow           *
  19.  *                              integrated "quantizer", "scalefactor" *
  20.  *                              combined window_samples routine into  *
  21.  *                              filter samples                        *
  22.  * 3/31/91  Bill Aspromonte     replaced read_filter by               *
  23.  *                              create_syn_filter and introduced a    *
  24.  *                              new Sub-Band Synthesis routine called *
  25.  *                              SubBandSynthesis()                    *
  26.  * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
  27.  *                              Changed "out_fifo()" so that last     *
  28.  *                              unfilled block is also written out.   *
  29.  *                              "create_syn_filter()" was modified so *
  30.  *                              that calculation precision is same as *
  31.  *                              in specification tables.              *
  32.  *                              Changed "decode_scale()" to reflect   *
  33.  *                              specifications.                       *
  34.  *                              Removed all routines used by          *
  35.  *                              "synchronize_buffer()".  This is now  *
  36.  *                              replaced by "seek_sync()".            *
  37.  *                              Incorporated Jean-Georges Fritsch's   *
  38.  *                              "bitstream.c" package.                *
  39.  *                              Deleted "reconstruct_sample()".       *
  40.  * 27jun91  dpwe (Aware)        Passed outFile and &sampFrames as     *
  41.  *                              args to out_fifo() - were global.     *
  42.  *                              Moved "alloc_*" reader to common.c.   *
  43.  *                              alloc, sblimit, stereo passed via new *
  44.  *                              'frame_params struct (were globals).  *
  45.  *                              Added JOINT STEREO decoding, lyrs I&II*
  46.  *                              Affects: decode_bitalloc,buffer_samps *
  47.  *                              Plus a few other cleanups.            *
  48.  * 6/10/91   Earle Jennings     conditional expansion added in        *
  49.  *                              II_dequantize_sample to handle range  *
  50.  *                              problems in MSDOS version             *
  51.  * 8/8/91    Jens Spille        Change for MS-C6.00                   *
  52.  *10/1/91    S.I. Sudharsanan,  Ported to IBM AIX platform.           *
  53.  *           Don H. Lee,                                              *
  54.  *           Peter W. Farrett                                         *
  55.  *10/3/91    Don H. Lee         implemented CRC-16 error protection   *
  56.  *                              newly introduced functions are        *
  57.  *                              buffer_CRC and recover_CRC_error.     *
  58.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  59.  *                              important fixes involved changing     *
  60.  *                              16-bit ints to long or unsigned in    *
  61.  *                              bit alloc routines for quant of 65535 *
  62.  *                              and passing proper function args.     *
  63.  *                              Removed "Other Joint Stereo" option   *
  64.  *                              and made bitrate be total channel     *
  65.  *                              bitrate, irrespective of the mode.    *
  66.  *                              Fixed many small bugs & reorganized.  *
  67.  * 7/27/92  Juan Pineda         Bug fix in SubBandSynthesis()         *
  68.  **********************************************************************/
  69.  
  70. #include        "common.h"
  71. #include        "decoder.h"
  72.  
  73. /***************************************************************
  74. /*
  75. /* This module contains the core of the decoder ie all the
  76. /* computational routines. (Layer I and II only)
  77. /* Functions are common to both layer unless
  78. /* otherwise specified.
  79. /*
  80. /***************************************************************/
  81.  
  82. /*****************************************************************
  83. /*
  84. /* The following routines decode the system information
  85. /*
  86. /****************************************************************/
  87.  
  88. /************ Layer I, Layer II & Layer III ******************/
  89.  
  90. void decode_info(bs, fr_ps)
  91. Bit_stream_struc *bs;
  92. frame_params *fr_ps;
  93. {
  94.     layer *hdr = fr_ps->header;
  95.  
  96.     hdr->version = get1bit(bs);
  97.     hdr->lay = 4-getbits(bs,2);
  98.     hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */
  99.     hdr->bitrate_index = getbits(bs,4);
  100.     hdr->sampling_frequency = getbits(bs,2);
  101.     hdr->padding = get1bit(bs);
  102.     hdr->extension = get1bit(bs);
  103.     hdr->mode = getbits(bs,2);
  104.     hdr->mode_ext = getbits(bs,2);
  105.     hdr->copyright = get1bit(bs);
  106.     hdr->original = get1bit(bs);
  107.     hdr->emphasis = getbits(bs,2);
  108. }
  109.  
  110. /*******************************************************************
  111. /*
  112. /* The bit allocation information is decoded. Layer I
  113. /* has 4 bit per subband whereas Layer II is Ws and bit rate
  114. /* dependent.
  115. /*
  116. /********************************************************************/
  117.  
  118. /**************************** Layer II *************/
  119.  
  120. void II_decode_bitalloc(bs, bit_alloc, fr_ps)
  121. Bit_stream_struc *bs;
  122. unsigned int bit_alloc[2][SBLIMIT];
  123. frame_params *fr_ps;
  124. {
  125.     int i,j;
  126.     int stereo = fr_ps->stereo;
  127.     int sblimit = fr_ps->sblimit;
  128.     int jsbound = fr_ps->jsbound;
  129.     al_table *alloc = fr_ps->alloc;
  130.  
  131.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  132.         bit_alloc[j][i] = (char) getbits(bs,(*alloc)[i][0].bits);
  133.  
  134.     for (i=jsbound;i<sblimit;i++) /* expand to 2 channels */
  135.         bit_alloc[0][i] = bit_alloc[1][i] =
  136.             (char) getbits(bs,(*alloc)[i][0].bits);
  137.  
  138.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  139.         bit_alloc[j][i] = 0;
  140. }
  141.  
  142. /**************************** Layer I *************/
  143.  
  144. void I_decode_bitalloc(bs, bit_alloc, fr_ps)
  145. Bit_stream_struc *bs;
  146. unsigned int bit_alloc[2][SBLIMIT];
  147. frame_params *fr_ps;
  148. {
  149.     int i,j;
  150.     int stereo  = fr_ps->stereo;
  151.     int sblimit = fr_ps->sblimit;
  152.     int jsbound = fr_ps->jsbound;
  153.     int b;
  154.  
  155.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  156.         bit_alloc[j][i] = getbits(bs,4);
  157.     for (i=jsbound;i<SBLIMIT;i++) {
  158.         b = getbits(bs,4);
  159.         for (j=0;j<stereo;j++)
  160.             bit_alloc[j][i] = b;
  161.     }
  162. }
  163.  
  164. /*****************************************************************
  165. /*
  166. /* The following two functions implement the layer I and II
  167. /* format of scale factor extraction. Layer I involves reading
  168. /* 6 bit per subband as scale factor. Layer II requires reading
  169. /* first the scfsi which in turn indicate the number of scale factors
  170. /* transmitted.
  171. /*    Layer I : I_decode_scale
  172. /*   Layer II : II_decode_scale
  173. /*
  174. /****************************************************************/
  175.  
  176. /************************** Layer I stuff ************************/
  177.  
  178. void I_decode_scale(bs, bit_alloc, scale_index, fr_ps)
  179. Bit_stream_struc *bs;
  180. unsigned int bit_alloc[2][SBLIMIT], scale_index[2][3][SBLIMIT];
  181. frame_params *fr_ps;
  182. {
  183.     int i,j;
  184.     int stereo = fr_ps->stereo;
  185.     int sblimit = fr_ps->sblimit;
  186.  
  187.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  188.         if (!bit_alloc[j][i])
  189.             scale_index[j][0][i] = SCALE_RANGE-1;
  190.         else                    /* 6 bit per scale factor */
  191.             scale_index[j][0][i] =  getbits(bs,6);
  192.  
  193. }
  194.  
  195. /*************************** Layer II stuff ***************************/
  196.  
  197. void II_decode_scale(bs,scfsi, bit_alloc,scale_index, fr_ps)
  198. Bit_stream_struc *bs;
  199. unsigned int scfsi[2][SBLIMIT], bit_alloc[2][SBLIMIT],
  200.              scale_index[2][3][SBLIMIT];
  201. frame_params *fr_ps;
  202. {
  203.     int i,j;
  204.     int stereo = fr_ps->stereo;
  205.     int sblimit = fr_ps->sblimit;
  206.    
  207.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) /* 2 bit scfsi */
  208.         if (bit_alloc[j][i]) scfsi[j][i] = (char) getbits(bs,2);
  209.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)   
  210.         scfsi[j][i] = 0;
  211.  
  212.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  213.         if (bit_alloc[j][i])   
  214.             switch (scfsi[j][i]) {
  215.                 /* all three scale factors transmitted */
  216.              case 0 : scale_index[j][0][i] = getbits(bs,6);
  217.                 scale_index[j][1][i] = getbits(bs,6);
  218.                 scale_index[j][2][i] = getbits(bs,6);
  219.                 break;
  220.                 /* scale factor 1 & 3 transmitted */
  221.              case 1 : scale_index[j][0][i] =
  222.                  scale_index[j][1][i] = getbits(bs,6);
  223.                 scale_index[j][2][i] = getbits(bs,6);
  224.                 break;
  225.                 /* scale factor 1 & 2 transmitted */
  226.              case 3 : scale_index[j][0][i] = getbits(bs,6);
  227.                 scale_index[j][1][i] =
  228.                     scale_index[j][2][i] =  getbits(bs,6);
  229.                 break;
  230.                 /* only one scale factor transmitted */
  231.              case 2 : scale_index[j][0][i] =
  232.                  scale_index[j][1][i] =
  233.                      scale_index[j][2][i] = getbits(bs,6);
  234.                 break;
  235.                 default : break;
  236.             }
  237.         else {
  238.             scale_index[j][0][i] = scale_index[j][1][i] =
  239.                 scale_index[j][2][i] = SCALE_RANGE-1;
  240.         }
  241.     }
  242.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) {
  243.         scale_index[j][0][i] = scale_index[j][1][i] =
  244.             scale_index[j][2][i] = SCALE_RANGE-1;
  245.     }
  246. }
  247.  
  248. /**************************************************************
  249. /*
  250. /*   The following two routines take care of reading the
  251. /* compressed sample from the bit stream for both layer 1 and
  252. /* layer 2. For layer 1, read the number of bits as indicated
  253. /* by the bit_alloc information. For layer 2, if grouping is
  254. /* indicated for a particular subband, then the sample size has
  255. /* to be read from the bits_group and the merged samples has
  256. /* to be decompose into the three distinct samples. Otherwise,
  257. /* it is the same for as layer one.
  258. /*
  259. /**************************************************************/
  260.  
  261. /******************************* Layer I stuff ******************/
  262.  
  263. void I_buffer_sample(bs, sample, bit_alloc, fr_ps)
  264. unsigned int FAR sample[2][3][SBLIMIT];
  265. unsigned int bit_alloc[2][SBLIMIT];
  266. Bit_stream_struc *bs;
  267. frame_params *fr_ps;
  268. {
  269.     int i,j,k;
  270.     int stereo = fr_ps->stereo;
  271.     int sblimit = fr_ps->sblimit;
  272.     int jsbound = fr_ps->jsbound;
  273.     unsigned int s;
  274.  
  275.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  276.         if ( (k = bit_alloc[j][i]) == 0)
  277.             sample[j][0][i] = 0;
  278.         else 
  279.             sample[j][0][i] = (unsigned int) getbits(bs,k+1);
  280.     for (i=jsbound;i<SBLIMIT;i++) {
  281.         if ( (k = bit_alloc[0][i]) == 0)
  282.             s = 0;
  283.         else 
  284.             s = (unsigned int)getbits(bs,k+1);
  285.         for (j=0;j<stereo;j++)
  286.             sample[j][0][i]    = s;
  287.     }
  288. }
  289.  
  290. /*************************** Layer II stuff ************************/
  291.  
  292. void II_buffer_sample(bs,sample,bit_alloc,fr_ps)
  293. unsigned int FAR sample[2][3][SBLIMIT];
  294. unsigned int bit_alloc[2][SBLIMIT];
  295. Bit_stream_struc *bs;
  296. frame_params *fr_ps;
  297. {
  298.     int i,j,k,m;
  299.     int stereo = fr_ps->stereo;
  300.     int sblimit = fr_ps->sblimit;
  301.     int jsbound = fr_ps->jsbound;
  302.     al_table *alloc = fr_ps->alloc;
  303.  
  304.     for (i=0;i<sblimit;i++) for (j=0;j<((i<jsbound)?stereo:1);j++) {
  305.         if (bit_alloc[j][i]) {
  306.             /* check for grouping in subband */
  307.             if ((*alloc)[i][bit_alloc[j][i]].group==3)
  308.                 for (m=0;m<3;m++) {
  309.                     k = (*alloc)[i][bit_alloc[j][i]].bits;
  310.                     sample[j][m][i] = (unsigned int) getbits(bs,k);
  311.                 }         
  312.             else {              /* bit_alloc = 3, 5, 9 */
  313.                 unsigned int nlevels, c=0;
  314.  
  315.                 nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  316.                 k=(*alloc)[i][bit_alloc[j][i]].bits;
  317.                 c = (unsigned int) getbits(bs, k);
  318.                 for (k=0;k<3;k++) {
  319.                     sample[j][k][i] = c % nlevels;
  320.                     c /= nlevels;
  321.                 }
  322.             }
  323.         }
  324.         else {                  /* for no sample transmitted */
  325.             for (k=0;k<3;k++) sample[j][k][i] = 0;
  326.         }
  327.         if(stereo == 2 && i>= jsbound) /* joint stereo : copy L to R */
  328.             for (k=0;k<3;k++) sample[1][k][i] = sample[0][k][i];
  329.     }
  330.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) for (k=0;k<3;k++)
  331.         sample[j][k][i] = 0;
  332. }      
  333.  
  334. /**************************************************************
  335. /*
  336. /*   Restore the compressed sample to a factional number.
  337. /*   first complement the MSB of the sample
  338. /*    for layer I :
  339. /*    Use s = (s' + 2^(-nb+1) ) * 2^nb / (2^nb-1)
  340. /*   for Layer II :
  341. /*   Use the formula s = s' * c + d
  342. /*
  343. /**************************************************************/
  344.  
  345. static double c[17] = { 1.33333333333, 1.60000000000, 1.14285714286,
  346.                         1.77777777777, 1.06666666666, 1.03225806452,
  347.                         1.01587301587, 1.00787401575, 1.00392156863,
  348.                         1.00195694716, 1.00097751711, 1.00048851979,
  349.                         1.00024420024, 1.00012208522, 1.00006103888,
  350.                         1.00003051851, 1.00001525902 };
  351.  
  352. static double d[17] = { 0.500000000, 0.500000000, 0.250000000, 0.500000000,
  353.                         0.125000000, 0.062500000, 0.031250000, 0.015625000,
  354.                         0.007812500, 0.003906250, 0.001953125, 0.0009765625,
  355.                         0.00048828125, 0.00024414063, 0.00012207031,
  356.                         0.00006103516, 0.00003051758 };
  357.  
  358. /************************** Layer II stuff ************************/
  359.  
  360. void II_dequantize_sample(sample, bit_alloc, fraction, fr_ps)
  361. unsigned int FAR sample[2][3][SBLIMIT];
  362. unsigned int bit_alloc[2][SBLIMIT];
  363. double FAR fraction[2][3][SBLIMIT];
  364. frame_params *fr_ps;
  365. {
  366.     int i, j, k, x;
  367.     int stereo = fr_ps->stereo;
  368.     int sblimit = fr_ps->sblimit;
  369.     al_table *alloc = fr_ps->alloc;
  370.  
  371.     for (i=0;i<sblimit;i++)  for (j=0;j<3;j++) for (k=0;k<stereo;k++)
  372.         if (bit_alloc[k][i]) {
  373.  
  374.             /* locate MSB in the sample */
  375.             x = 0;
  376. #ifndef MS_DOS
  377.             while ((1L<<x) < (*alloc)[i][bit_alloc[k][i]].steps) x++;
  378. #else
  379.             /* microsoft C thinks an int is a short */
  380.             while (( (unsigned long) (1L<<(long)x) <
  381.                     (unsigned long)( (*alloc)[i][bit_alloc[k][i]].steps)
  382.                     ) && ( x < 16) ) x++;
  383. #endif
  384.  
  385.             /* MSB inversion */
  386.             if (((sample[k][j][i] >> x-1) & 1) == 1)
  387.                 fraction[k][j][i] = 0.0;
  388.             else  fraction[k][j][i] = -1.0;
  389.  
  390.             /* Form a 2's complement sample */
  391.             fraction[k][j][i] += (double) (sample[k][j][i] & ((1<<x-1)-1)) /
  392.                 (double) (1L<<x-1);
  393.  
  394.             /* Dequantize the sample */
  395.             fraction[k][j][i] += d[(*alloc)[i][bit_alloc[k][i]].quant];
  396.             fraction[k][j][i] *= c[(*alloc)[i][bit_alloc[k][i]].quant];
  397.         }
  398.         else fraction[k][j][i] = 0.0;   
  399.    
  400.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<3;j++) for(k=0;k<stereo;k++)
  401.         fraction[k][j][i] = 0.0;
  402. }
  403.  
  404. /***************************** Layer I stuff ***********************/
  405.  
  406. void I_dequantize_sample(sample, fraction, bit_alloc, fr_ps)
  407. unsigned int FAR sample[2][3][SBLIMIT];
  408. unsigned int bit_alloc[2][SBLIMIT];
  409. double FAR fraction[2][3][SBLIMIT];
  410. frame_params *fr_ps;
  411. {
  412.     int i, nb, k;
  413.     int stereo = fr_ps->stereo;
  414.     int sblimit = fr_ps->sblimit;
  415.  
  416.     for (i=0;i<SBLIMIT;i++)
  417.         for (k=0;k<stereo;k++)
  418.             if (bit_alloc[k][i]) {
  419.                 nb = bit_alloc[k][i] + 1;
  420.                 if (((sample[k][0][i] >> nb-1) & 1) == 1) fraction[k][0][i] = 0.0;
  421.                 else fraction[k][0][i] = -1.0;
  422.                 fraction[k][0][i] += (double) (sample[k][0][i] & ((1<<nb-1)-1)) /
  423.                     (double) (1L<<nb-1);
  424.  
  425.                 fraction[k][0][i] =
  426.                     (double) (fraction[k][0][i]+1.0/(double)(1L<<nb-1)) *
  427.                         (double) (1L<<nb) / (double) ((1L<<nb)-1);
  428.             }
  429.             else fraction[k][0][i] = 0.0;
  430. }
  431.  
  432. /************************************************************
  433. /*
  434. /*   Restore the original value of the sample ie multiply
  435. /*    the fraction value by its scalefactor.
  436. /*
  437. /************************************************************/
  438.  
  439. /************************* Layer II Stuff **********************/
  440.  
  441. void II_denormalize_sample(fraction, scale_index,fr_ps,x)
  442. double FAR fraction[2][3][SBLIMIT];
  443. unsigned int scale_index[2][3][SBLIMIT];
  444. frame_params *fr_ps;
  445. int x;
  446. {
  447.     int i,j,k;
  448.     int stereo = fr_ps->stereo;
  449.     int sblimit = fr_ps->sblimit;
  450.  
  451.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  452.         fraction[j][0][i] *= multiple[scale_index[j][x][i]];
  453.         fraction[j][1][i] *= multiple[scale_index[j][x][i]];
  454.         fraction[j][2][i] *= multiple[scale_index[j][x][i]];
  455.     }
  456. }
  457.  
  458. /**************************** Layer I stuff ******************************/
  459.  
  460. void I_denormalize_sample(fraction,scale_index,fr_ps)
  461. double FAR fraction[2][3][SBLIMIT];
  462. unsigned int scale_index[2][3][SBLIMIT];
  463. frame_params *fr_ps;
  464. {
  465.     int i,j,k;
  466.     int stereo = fr_ps->stereo;
  467.     int sblimit = fr_ps->sblimit;
  468.  
  469.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  470.         fraction[j][0][i] *= multiple[scale_index[j][0][i]];
  471. }
  472.  
  473. /*****************************************************************
  474. /*
  475. /* The following are the subband synthesis routines. They apply
  476. /* to both layer I and layer II stereo or mono. The user has to
  477. /* decide what parameters are to be passed to the routines.
  478. /*
  479. /***************************************************************/
  480.  
  481. /*************************************************************
  482. /*
  483. /*   Pass the subband sample through the synthesis window
  484. /*
  485. /**************************************************************/
  486.  
  487. /* create in synthesis filter */
  488.  
  489. void create_syn_filter(filter)
  490. double FAR filter[64][SBLIMIT];
  491. {
  492.     register int i,k;
  493.  
  494.     for (i=0; i<64; i++)
  495.         for (k=0; k<32; k++) {
  496.             if ((filter[i][k] = 1e9*cos((double)((PI64*i+PI4)*(2*k+1)))) >= 0)
  497.                 modf(filter[i][k]+0.5, &filter[i][k]);
  498.             else
  499.                 modf(filter[i][k]-0.5, &filter[i][k]);
  500.             filter[i][k] *= 1e-9;
  501.         }
  502. }
  503.  
  504. /***************************************************************
  505. /*
  506. /*   Window the restored sample
  507. /*
  508. /***************************************************************/
  509.  
  510. /* read in synthesis window */
  511.  
  512. void read_syn_window(window)
  513. double FAR window[HAN_SIZE];
  514. {
  515.     int i,j[4];
  516.     FILE *fp;
  517.     double f[4];
  518.     char t[150];
  519.  
  520.     if (!(fp = OpenTableFile("dewindow") )) {
  521.         printf("Please check synthesis window table 'dewindow'\n");
  522.         exit(1);
  523.     }
  524.     for (i=0;i<512;i+=4) {
  525.         fgets(t, 150, fp);
  526.         sscanf(t,"D[%d] = %lf D[%d] = %lf D[%d] = %lf D[%d] = %lf\n",
  527.                j, f,j+1,f+1,j+2,f+2,j+3,f+3);
  528.         if (i==j[0]) {
  529.             window[i] = f[0];
  530.             window[i+1] = f[1];
  531.             window[i+2] = f[2];
  532.             window[i+3] = f[3];
  533.         }
  534.         else {
  535.             printf("Check index in synthesis window table\n");
  536.             exit(1);
  537.         }
  538.         fgets(t,150,fp);
  539.     }
  540.     fclose(fp);
  541. }
  542.  
  543. int SubBandSynthesis (bandPtr, channel, samples)
  544. double *bandPtr;
  545. int channel;
  546. short *samples;
  547. {
  548.     register int i,j,k;
  549.     register double *bufOffsetPtr, sum;
  550.     static int init = 1;
  551.     typedef double NN[64][32];
  552.     static NN FAR *filter;
  553.     typedef double BB[2][2*HAN_SIZE];
  554.     static BB FAR *buf;
  555.     static int bufOffset = 64;
  556.     static double FAR *window;
  557.     int clip = 0;               /* count & return how many samples clipped */
  558.  
  559.     if (init) {
  560.         buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");
  561.         filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");
  562.         create_syn_filter(*filter);
  563.         window = (double FAR *) mem_alloc(sizeof(double) * HAN_SIZE, "WIN");
  564.         read_syn_window(window);
  565.         bufOffset = 64;
  566.         init = 0;
  567.     }
  568.     if (channel == 0) bufOffset = (bufOffset - 64) & 0x3ff;
  569.     bufOffsetPtr = &((*buf)[channel][bufOffset]);
  570.  
  571.     for (i=0; i<64; i++) {
  572.         sum = 0;
  573.         for (k=0; k<32; k++)
  574.             sum += bandPtr[k] * (*filter)[i][k];
  575.         bufOffsetPtr[i] = sum;
  576.     }
  577.     /*  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)  */
  578.     /*  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)  */
  579.     for (j=0; j<32; j++) {
  580.         sum = 0;
  581.         for (i=0; i<16; i++) {
  582.             k = j + (i<<5);
  583.             sum += window[k] * (*buf) [channel] [( (k + ( ((i+1)>>1) <<6) ) +
  584.                                                   bufOffset) & 0x3ff];
  585.         }
  586.  
  587. /*   {long foo = (sum > 0) ? sum * SCALE + 0.5 : sum * SCALE - 0.5; */
  588.      {long foo = sum * SCALE;
  589.      if (foo >= (long) SCALE)      {samples[j] = SCALE-1; ++clip;}
  590.      else if (foo < (long) -SCALE) {samples[j] = -SCALE;  ++clip;}
  591.      else                           samples[j] = foo;
  592.  }
  593.     }
  594.     return(clip);
  595. }
  596.  
  597. void out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames)
  598. short FAR pcm_sample[2][3][SBLIMIT];
  599. int num;
  600. frame_params *fr_ps;
  601. int done;
  602. FILE *outFile;
  603. unsigned long *psampFrames;
  604. {
  605.     int i,j,l;
  606.     int stereo = fr_ps->stereo;
  607.     int sblimit = fr_ps->sblimit;
  608.     static short int outsamp[1600];
  609.     static long k = 0;
  610.  
  611.     if (!done)
  612.         for (i=0;i<num;i++) for (j=0;j<SBLIMIT;j++) {
  613.             (*psampFrames)++;
  614.             for (l=0;l<stereo;l++) {
  615.                 if (!(k%1600) && k) {
  616.                     fwrite(outsamp,2,1600,outFile);
  617.                     k = 0;
  618.                 }
  619.                 outsamp[k++] = pcm_sample[l][i][j];
  620.             }
  621.         }
  622.     else {
  623.         fwrite(outsamp,2,(int)k,outFile);
  624.         k = 0;
  625.     }
  626. }
  627.  
  628. void  buffer_CRC(bs, old_crc)
  629. Bit_stream_struc  *bs;
  630. unsigned int  *old_crc;
  631. {
  632.     *old_crc = getbits(bs, 16);
  633. }
  634.  
  635. void  recover_CRC_error(pcm_sample, error_count, fr_ps, outFile, psampFrames)
  636. short FAR pcm_sample[2][3][SBLIMIT];
  637. int error_count;
  638. frame_params *fr_ps;
  639. FILE *outFile;
  640. unsigned long *psampFrames;
  641. {
  642.     int  stereo = fr_ps->stereo;
  643.     int  num, done, i;
  644.     int  samplesPerFrame, samplesPerSlot;
  645.     layer  *hdr = fr_ps->header;
  646.     long  offset;
  647.     short  *temp;
  648.  
  649.     num = 3;
  650.     if (hdr->lay == 1) num = 1;
  651.  
  652.     samplesPerSlot = SBLIMIT * num * stereo;
  653.     samplesPerFrame = samplesPerSlot * 32;
  654.  
  655.     if (error_count == 1) {     /* replicate previous error_free frame */
  656.         done = 1;
  657.         /* flush out fifo */
  658.         out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  659.         /* go back to the beginning of the previous frame */
  660.         offset = sizeof(short int) * samplesPerFrame;
  661.         fseek(outFile, -offset, SEEK_CUR);
  662.         done = 0;
  663.         for (i = 0; i < SCALE_BLOCK; i++) {
  664.             fread(pcm_sample, 2, samplesPerSlot, outFile);
  665.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  666.         }
  667.     }
  668.     else{                       /* mute the frame */
  669.         temp = (short*) pcm_sample;
  670.         done = 0;
  671.         for (i = 0; i < 2*3*SBLIMIT; i++)
  672.             *temp++ = MUTE;     /* MUTE value is in decoder.h */
  673.         for (i = 0; i < SCALE_BLOCK; i++)
  674.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  675.     }
  676. }
  677.  
  678.